package liudan.archetype;

import com.mongodb.*;
import com.mongodb.util.JSON;
import org.bson.types.ObjectId;

import java.net.UnknownHostException;
import java.util.List;

/**
 * Description:
 * Email: liudan@ucfgroup.com
 * Title
 * Copyright: Copyright (c) 2017
 * User: liudan
 * Created on 2017/4/5 10:53
 */
public class MongoDBCURD {
    static Mongo mongo = null;

    static DB db = null;

    public MongoDBCURD(String dataBaseName) {
        try {
            mongo = new Mongo("127.0.0.1:27017");
            db = mongo.getDB(dataBaseName);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        MongoDBCURD mongoDBCURD = new MongoDBCURD("foobar");
        //创建集合
//        mongoDBCURD.createCollection("javadb");

        //插入数据
//        mongoDBCURD.insert("javadb");


//        List<DBObject> listOb = new ArrayList<DBObject>();
//        Map<String, Object> map = new HashMap<String, Object>();
//        map.put("name", "bb");
//        map.put("age", 30);
//        Map<String, Object> map2 = new HashMap<String, Object>();
//        map2.put("name", "cc");
//        map2.put("age", 33);
//        DBObject dbObject1 = new BasicDBObject(map);
//        DBObject dbObject2 = new BasicDBObject(map2);
//
//        listOb.add(dbObject1);
//        listOb.add(dbObject2);
//        //批量插入
//        mongoDBCURD.insertBatch(listOb, "javadb");

        //删除数据
//        mongoDBCURD.deleteById("","javadb");

        //更新数据
//        mongoDBCURD.update("javadb");

        //查询指定字段
        DBObject dbObject = new BasicDBObject();
        dbObject.put("_id", false);
        dbObject.put("name", true);
        dbObject.put("age", true);
        String result = mongoDBCURD.findNoPage(dbObject, "javadb");

        //查询所有
//        String result = mongoDBCURD.find("javadb");
        System.out.println("javadb:" + result);


    }

    private String findNoPage(DBObject dbObject, String collName) {
        DBCollection dbCollection = db.getCollection(collName);
        DBCursor dbCursor = dbCollection.find(null, dbObject);
        StringBuilder sbd = new StringBuilder();
        while (dbCursor.hasNext()) {
            sbd.append(JSON.serialize(dbCursor.next()));
        }
        return sbd.toString();
    }

    private void update(String collName) {
        DBCollection dbCollection = db.getCollection(collName);
        DBObject updateOb = new BasicDBObject();
        updateOb.put("$set", new BasicDBObject("email", "2649834095@qq.com"));
        dbCollection.update(new BasicDBObject(), updateOb, false, true);
    }

    private void deleteById(String s, String collName) {
        DBCollection dbCollection = db.getCollection(collName);
        DBObject dbObject = new BasicDBObject();
        dbObject.put("_id", new ObjectId("58e462b977c85ce4f35dd751"));
        dbCollection.remove(dbObject).getN();
    }

    private void insertBatch(List<DBObject> listOb, String collName) {
        DBCollection dbCollection = db.getCollection(collName);
        dbCollection.insert(listOb);
    }

    private String find(String collName) {

        DBCollection dbCollection = db.getCollection(collName);
        DBCursor dbCursor = dbCollection.find();
        StringBuilder sbd = new StringBuilder();
        while (dbCursor.hasNext()) {
            sbd.append(JSON.serialize(dbCursor.next()));
        }
        return sbd.toString();
    }

    private void insert(String javadb) {
        DBCollection dbCollection = db.getCollection("javadb");
        DBObject dbObject = new BasicDBObject();
        dbObject.put("name", "a");
        dbObject.put("age", 20);
        dbCollection.insert(dbObject);
    }


    private void createCollection(String collName) {
        DBObject dbObject = new BasicDBObject();
        db.createCollection(collName, dbObject);
    }
}
